<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);

class svn_auth extends admin {
	function __construct() {
		parent::__construct();
		$this->db = pc_base::load_model('svn_auth_model');
	}
	
	function ip(){
		exit();
		$str = '
test = wangyuqin,huangdajun,zhangfeng,dingyonghe,luochangquan,huangjingfu,linhaiyan,chenwenhuan,zhangjin,caoniudun,
doc = pengsanyuan
root = huangzhiquan,zhaoke,caoniudun,chenzhen
o2otest=zhuyan,liudongni,tianjie,zhujiadong,wangyuqin,luochangquan,huangjingfu,linhaiyan,yuxiong,chenfeng,hechuan,dengxianhai,linliangmou,lizhenhua,xietao,yangqiuhui
qa = hulingmei
dc = zhuoshuiyu

b2b = yangpeng,panchaoyuan,lihonglin,yuxinjian,duwei,tumaoqiang,leijunjie,chenwenyong,chenfeng

yun = caoniudun,xiejianxin,wubin,wangmengguang,chenwenyong,liwuyuan,chenkaisong,liupeng,zengpan,chenzhen,huangzhiquan,zhaoke,chenchaochao,chenjiefang,zhangyuhao,guguofeng,liuzhiqiong,lichengwu,zhuyangbo,chenhongrui,zhanghong,magongcai,zhangyun,zhangyong,pengsanyuan,liuhu,yangqiuhui,wangyuqin,huangdajun,zhangfeng,chenqiaofang,dingyonghe,liufei,lutao,chenwenhuan

mecp_gz = xiejianxin,wubin,wangmengguang,chenwenyong,zhaoke
mecp = caoniudun,pengsanyuan,wangyuqin,sunxuchang
mecp_prod = yangqiuhui,zhuoshuiyu
mecp_test = huangdajun,zhangfeng,huguobin,zhangjin,chenwenhuan,huangjingfu,linhaiyan
mecp_java = liwuyuan,chenzhen,huangzhiquan,chenchaochao,chenjiefang,zhangyuhao,lutao
mecp_app = liupeng,zengpan,guguofeng,liuzhiqiong,lichengwu,zhuyangbo,zhangjiqing,liuhu,huangchangyong,huangyujie


mecp_lz = fangcheng,guohai,jiangyuhuan,chenxueyu,wuzhichao,fuguojun,yuxiqiang,hgy,zhaopeng,linan,weiwei,wanghaohao,luoyun,dingguojie

mecp_gl = zengjie,liuzhiling,wuyongchao,chenhongrui,zhanghong,zhangyun,lisanchun,chengjinxiu,zhangyong,lixianjin,yichaowen,hefangyi,yangmingzhang

mecp_quit = magongcai,dingyonghe,liufei,gaobin


iecpay = suiyixiong,zhangxiaojie,zhangchen,caoniudun,tangshijun,wangsen,yichaowen,chenwenhuan,lutao,tangyunyu,yangming
iecbi = wangsen,niekang,szlibo,caoniudun,chenfu

o2o = dengxingxin,wangyuqin,baoguolun,hukeping,suiyixiong,zhangchen,tangshijun,zhangxiaojie,caoniudun,luochangquan,lina,yichaowen,lurongzhuan,lipengkun,zengjie,wuyongchao,liuzhiling,moyanjun,luojing,fengjun,zhujiadong,chenhongrui,lisanchun,glzhanghong,zhangyun,chengjinxiu,glzhangyong,zhangzengrui,menghongyuan,heqi,gzlibo,zhaoke,wubin,xiejianxin,wangxuehao,chenwenyong,lichengzhou,leijunjie,,taoyuliang,xialian,linpeiwei,zhuchaoyang,zengliang,xuyebin,lvlinqing,xuwenjin,linzaixian,zhengchangle,luqilei,huangjingfu,xuyang,jiangzhi,lutao,zhangfeng,zhangjin, jiyiping,tangyunyu,cdzhangxiaojie,huanghongrong,zhangyongxin,yuanlei,liping,lixuewen,molei,heli,mojingguo,zhenghong,yonglongwang,lijunnan,yangmingzhang

o2o_xy = jiyiping,wangyiming,gzlibo,tangshijun,liuxiaofei,wangmengguang,gonglina,hehua,denggaoqiang,huangfuxin,xiechangtao,zhaixu,liuzan,qinxiaozheng

foundation = xiejianxin,zhaoke,menghongyuan,wubin,zengliang,xuwenjin,xialian,zhengchangle,huangchangyong,chenwenyong,zhuchaoyang
uums =  xiejianxin,zhaoke,menghongyuan,zengliang,xialian,zhengchangle,huangchangyong
wuhang = houkun,zhangfan,liyongfeng,liuliu,zengweixiang,machao,liyulin,sunjianfeng,zhangbangxuan,liliang,zoulinheng,luguoming';
		
		$a = explode("\n",$str);
		$a = array_filter($a,'trim');
		$group_db = pc_base::load_model('svn_group_model');
		$user_db = pc_base::load_model('svn_user_model');
		print_r($a);
		//exit();
		$users = array();
		foreach($a as $v){
			$v = preg_replace('#\s#is', '', $v);
			$b = explode('=', $v);
			$b = array_filter($b,'trim');
			//print_r($b);
			//$gid = $group_db->insert(array('sid'=>6,'name'=>$b[0],'code'=>$b[0]));
			$c = explode(',',$b[1]);
			foreach($c as $u)
				$users[$u][] = $b[0];
			
			/*foreach($c as $u)
				$user_db->insert(array('user'=>$u,'created'=>time(),'proj'=>$b[0]));*/
		}
		foreach($users as $u=>$v){
			$user_db->insert(array('user'=>$u,'created'=>time(),'proj'=>implode(',',$v)));
		}
		print_r($users);
	}
	
	//svn list
	function init () {
		$infos = array();
			$id = intval($_GET['id']);
			$where = "";
			$page = $_GET['page'] ? $_GET['page'] : '1';
			$sql = "SELECT a.*,(SELECT GROUP_CONCAT(name) FROM  phpcms_svn_group WHERE FIND_IN_SET(id,a.groups)) as gnames FROM phpcms_svn_auth a WHERE sid=$id ORDER BY id ASC";
			//$list = $this->db->listinfo($where, $order = 'id ASC', $page, $pagesize = 20);
			$list = $this->db->query_info($sql, $page, $pagesize = 50);
			$pages = $this->db->pages;
			$show_dialog = true;
			$rws = $this->db->rws();
			$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m='.ROUTE_M.'&c='.ROUTE_C.'&a=add&id='.$id.'\', title:\'添加\', width:\'500\', height:\'300\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', '添加权限');
 			include $this->admin_tpl('svn/auth/list');
	}
	
	function add() {
		if(isset($_POST['dosubmit'])) {
			
			$info = $_POST['info'];
			$sid = intval($_POST['sid']);
			//$one = $this->db->get_one("path='".$info['path']."' AND sid = ".$sid);
			//if(!empty($one))showmessage('路径已存在');
			$rw = 1;
			if(isset($_POST['rw']) && is_array($_POST['rw']))$rw = array_sum($_POST['rw']);
			$info['rw'] = $rw;
			$groups = '';
			if(isset($_POST['groups']) && is_array($_POST['groups']))$groups = implode(',', $_POST['groups']);
			$info['groups'] = $groups;
			$info['sid'] = $sid;
			$info['created'] = time();
			$this->db->insert($info);
			
			//结束
			showmessage(L('operation_success'), '', '', 'add');
		} else {
			$id = intval($_GET['id']);
			$group_db = pc_base::load_model('svn_group_model');
			$group_list = $group_db->select('sid=0 OR sid='.$id);
			
			include $this->admin_tpl('svn/auth/add');
		}
	}
	function delete() {
		$_GET['id'] = intval($_GET['id']);
		$this->db->delete(array('id'=>$_GET['id']));
		showmessage(L('operation_success'));
	}
	
	function edit() {
		if(isset($_POST['dosubmit'])) {
			$id = intval($_POST['id']);
			
			$info = $_POST['info'];
			$sid = intval($_POST['sid']);
			//$one = $this->db->get_one("path='".$info['path']."' AND sid = ".$sid.' AND id<>'.$id);
			//if(!empty($one))showmessage('路径已存在');
			$rw = 1;
			if(isset($_POST['rw']) && is_array($_POST['rw']))$rw = array_sum($_POST['rw']);
			$info['rw'] = $rw;
			$groups = '';
			if(isset($_POST['groups']) && is_array($_POST['groups']))$groups = implode(',', $_POST['groups']);
			$info['groups'] = $groups;
			
			
			
			$this->db->update($info,array('id'=>$id));
			
			//结束语言文件修改
			showmessage(L('operation_success'), '', '', 'edit');
		} else {
			
			
			$id = intval($_GET['id']);
			$r = $this->db->get_one(array('id'=>$id));
			if($r) extract($r);
			$group_db = pc_base::load_model('svn_group_model');
			$group_list = $group_db->select('sid=0 OR sid='.$sid);
			$a_gids = explode(',', $r['groups']);
			$r_ids = array(1=>array(1),2=>array(2),3=>array(1,2));
			
			
			include $this->admin_tpl('svn/auth/edit');
		}
	}
	
	function authd(){
		$auths = array();
		//分组
		$auths[] = '[groups]';
		$group_db = pc_base::load_model('svn_group_model');
		$group_list = $group_db->select();
		
		$svn_db = pc_base::load_model('svn_model');
		$svn_list = $svn_db->array2map($svn_db->select(),'id','code');
			
		if($group_list)foreach ($group_list as $group){
			$sql = "SELECT GROUP_CONCAT(user) users FROM `phpcms_svn_user` WHERE FIND_IN_SET(".$group['id'].",groups)";
			$r = $group_db->query_one($sql);
			$parts = array();
			if($group['sid'])$parts[] = $svn_list[$group['sid']];
			$parts[] = $group['code'];
			$auths[] = implode('_',$parts). ' = ' .$r['users'];
		}
		//权限
		$auth_db = pc_base::load_model('svn_auth_model');
		$list = $auth_db->query_info("SELECT a.*,b.code FROM phpcms_svn_auth a ,phpcms_svn b WHERE a.sid=b.id order by a.sid asc");
		$r_ids = array(1=>'r',2=>'w',3=>'rw');
		$group_codes = $auth_db->array2map($group_list,'id');
		$paths = array();//路径
		$pathas = array();//权限
		if($list)foreach ($list as $v){
			$mk = md5('['.$v['code'].':'.$v['path'].']');
			if(!isset($paths[$mk]))$paths[$mk] = '['.$v['code'].':'.$v['path'].']';
			if(!isset($pathas))$pathas[$mk] = array();
			$rw = $r_ids[$v['rw']]? $r_ids[$v['rw']]:'';
			if($v['groups']){
				$a = explode(',',$v['groups']);
				foreach($a as $gid){
					$gcode = $group_codes[$gid];
					$parts = array();
					if($gcode['sid'])$parts[] = $svn_list[$gcode['sid']];
					$parts[] = $gcode['code'];
					if($gcode)$pathas[$mk][] = '@'. implode('_',$parts) . ' = ' . $rw;
				}
			}else{
				$pathas[$mk][] = '* = '.$rw;
			}
			
		}
		foreach ($paths as $k=>$v){
			$auths[] = $v;
			if($pathas[$k])foreach ($pathas[$k] as $av){
				$auths[] = $av;
			}
		}
		
		$auths = implode("\r\n",$auths);
		if($_GET['sync']){
			$this->db->delete(array('id'=>$_GET['id']));
			$svnServ = pc_base::load_sys_class('svnserv');
			$ret = $svnServ->upAuth($auths);
			
			$db = pc_base::load_model('svn_conf_model');
			if(!$ret['code'])$db->insert(array('created'=>time(),'updated'=>time(),'vkey'=>'auth','data'=>$ret['data']));
			showmessage(L('operation_success'));
		}
		
		include $this->admin_tpl('svn/auth/auth');
	}
	
	
}